import ourcity.utils.image_processing as ip
from django.db import models

LEVELS = (
    ('L', 'Local'),
    ('S', 'State'),
    ('F', 'Federal'),
)

class DistrictType(models.Model):
    name = models.CharField(max_length=64)
    
    def __unicode__(self):
        return self.name


class DistrictSubType(models.Model):
    name = models.CharField(max_length=64)
    
    def __unicode__(self):
        return self.name


class District(models.Model):
    type = models.ForeignKey(DistrictType)
    subtype = models.ForeignKey(DistrictSubType, blank=True, null=True)
    name = models.CharField(max_length=64, help_text='From Cicero DistrictID')
    city = models.CharField(max_length=64, blank=True)
    state = models.CharField(max_length=2, blank=True)
    country = models.CharField(max_length=64)

    def __unicode__(self):
        return u'%s: %s %s' % (self.type, self.name, self.city)


class Office(models.Model):
    name = models.CharField(max_length=64)

    def __unicode__(self):
        return self.name


class Party(models.Model):
    name = models.CharField(max_length=64)
    initial = models.CharField(max_length=1, blank=True)

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'parties'


class Donor(models.Model):
    name = models.CharField(max_length=64)
    description = models.TextField()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('name', 'id')


class RepManager(models.Manager):
    def active(self):
        return self.filter(active=True)


class Representative(models.Model):
    # fields specific to OurCity
    active = models.BooleanField(default=True)
    #representative = models.BooleanField(default=True)
    #candidate = models.BooleanField()
    photo = models.ImageField(upload_to='uploaded/rep_photos', blank=True)
    info = models.TextField(blank=True)
    donor_years = models.CharField(max_length=32, blank=True,
        help_text='either something like "2006-2008" or just "2009"')
    donors = models.ManyToManyField(Donor, through='Donation')
    # candidate fields
    candidate_election = models.ForeignKey('elections.Election', blank=True, 
            null=True, related_name='candidates')
    candidate_district = models.ForeignKey(District, blank=True, null=True, 
            help_text='district a candidate is running in', 
            related_name='candidates')
    candidate_office = models.ForeignKey(Office, blank=True, null=True, 
            related_name='candidates')
    candidate_party = models.ForeignKey(Party, blank=True, null=True, 
            related_name='candidates')
    # fields below mapped to cicero api (except district stuff, in District)
    assembly_name = models.CharField(max_length=64, blank=True)
    committees = models.TextField(blank=True)
    country = models.CharField(max_length=64, blank=True)
    description = models.TextField(blank=True)
    district = models.ForeignKey(District, related_name='reps', blank=True, 
            null=True)
    elected_official_id = models.CharField(max_length=64, blank=True)
    email1 = models.EmailField(blank=True)
    email2 = models.EmailField(blank=True)
    first_name = models.CharField(max_length=64)
    last_name = models.CharField(max_length=64)
    last_update_date = models.DateTimeField(blank=True, null=True)
    middle_initial = models.CharField(max_length=32, blank=True)
    name_suffix = models.CharField(max_length=32, blank=True)
    party = models.ForeignKey(Party, blank=True, null=True)
    primary_address1 = models.CharField(max_length=64, blank=True)
    primary_address2 = models.CharField(max_length=64, blank=True)
    primary_address3 = models.CharField(max_length=64, blank=True)
    primary_city = models.CharField(max_length=64, blank=True)
    primary_fax1 = models.CharField(max_length=64, blank=True)
    primary_fax2 = models.CharField(max_length=64, blank=True)
    primary_phone1 = models.CharField(max_length=64, blank=True)
    primary_phone2 = models.CharField(max_length=64, blank=True)
    primary_postal_code = models.CharField(max_length=64, blank=True)
    primary_state = models.CharField(max_length=64, blank=True)
    salutation = models.CharField(max_length=32, blank=True)
    secondary_address1 = models.CharField(max_length=64, blank=True)
    secondary_address2 = models.CharField(max_length=64, blank=True)
    secondary_address3 = models.CharField(max_length=64, blank=True)
    secondary_city = models.CharField(max_length=64, blank=True)
    secondary_fax1 = models.CharField(max_length=64, blank=True)
    secondary_fax2 = models.CharField(max_length=64, blank=True)
    secondary_phone1 = models.CharField(max_length=64, blank=True)
    secondary_phone2 = models.CharField(max_length=64, blank=True)
    secondary_postal_code = models.CharField(max_length=64, blank=True)
    secondary_state = models.CharField(max_length=64, blank=True)
    term_end_date = models.DateTimeField(blank=True, null=True)
    term_end_date_precision = models.CharField(max_length=1, blank=True)
    term_start_date = models.DateTimeField(blank=True, null=True)
    term_start_date_precision = models.CharField(max_length=1, blank=True)
    title = models.CharField(max_length=64, blank=True)
    office = models.ForeignKey(Office, blank=True, null=True)
    url1 = models.URLField(blank=True)
    url2 = models.URLField(blank=True)
    # all following are deprecated
    full_name = models.CharField(max_length=64)
    lookup_string = models.CharField(max_length=128)
    level = models.CharField(max_length=1, choices=LEVELS)
    phone = models.CharField(max_length=64, blank=True)
    fax = models.CharField(max_length=64, blank=True)
    website = models.URLField(blank=True)
    email = models.EmailField(blank=True)
    address = models.TextField(blank=True)

    objects = RepManager()

    def __unicode__(self):
        return u'%s %s %s' % (self.office, self.first_name, self.last_name)

    def get_full_name(self):
        return ' '.join((self.first_name, self.middle_initial, self.last_name))

    def get_district_name(self, candidate=False):
        if candidate:
            district = self.candidate_district
        else:
            district = self.district
        if not district:
            return ''
        name = district.name
        if name.isnumeric():
            return 'District %s' % name
        elif len(name) > 2:  # not a state abbreviation
            return ' '.join([x.capitalize() for x in name.strip().split()])
        else:
            return name

    def get_candidate_district_name(self):
        return self.get_district_name(candidate=True)

    def save(self, *args, **kwargs):
        super(Representative, self).save(*args, **kwargs)
        if self.photo:
            ip.resize_image(self.photo.path, 170, 204)

    def simple_save(self, *args, **kwargs):
        super(Representative, self).save(*args, **kwargs)

    class Meta:
        ordering = ('last_name', 'first_name', 'id')


class Donation(models.Model):
    representative = models.ForeignKey(Representative)
    donor = models.ForeignKey(Donor)
    amount = models.PositiveIntegerField()

    def __unicode__(self):
        return u'%s from %s to %s' % (self.amount, self.donor, 
                self.representative)


